package org.shadow.core.key;

import org.shadow.core.key.exception.InvalidSequenceException;

/**
 * 
 * @author zhouw
 * @version 0.1
 * @since 2016.12.02
 */
public abstract class AbstractKeyGenerator implements KeyGenerator {
	
	/** 主键类型 */
	private String keyType;
	
	/** 主键格式 */
	private String pattern;
	
	/** 增长步长 */
	private int step = 1;

	/** 主键格式化类 */
	private KeyFormatter formatter;
	
	@Override
	public String generate() throws InvalidSequenceException {
		int currentSequence = getCurrentSequence(step);
		formatter.setPattern(getPattern());
		return formatter.format(currentSequence);
	}
	
	public AbstractKeyGenerator() {
		this.formatter = new SimpleKeyFormatter();
	}
	
	abstract protected int getCurrentSequence(int step) throws InvalidSequenceException;

	public String getPattern() {
		return pattern;
	}
	
	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	public int getStep() {
		return step;
	}

	public void setStep(int step) {
		this.step = step;
	}

	public String getKeyType() {
		return keyType;
	}

	public void setKeyType(String keyType) {
		this.keyType = keyType;
	}
}
